// pages/reportCheck/reportCheck.js
const app=getApp()
const db = wx.cloud.database()
const _ = db.command

import { areaList } from '@vant/area-data';
Page({

  data: {
    stamp:440106,
    phone:"15876705385",
    currentDate:new Date().getTime(),
    minDate:new Date(2020, 11, 31).getTime(),
    maxDate: new Date(2099, 11, 31).getTime(),
    name:"",
    descri:"",
    time:"",

    contentList:[],
    uncheckContentList:[],
    loading:false
  },
  zhanOrShou(e){
    const index=e.currentTarget.dataset.index
    if(e.currentTarget.dataset.uncheck){
      this.setData({
        [`uncheckContentList[${index}].zhanshou`]:!this.data.uncheckContentList[index].zhanshou
      })
    }else{
      this.setData({
        [`contentList[${index}].zhanshou`]:!this.data.contentList[index].zhanshou
      })
    }
  },
  choosename(){
    this.animate('.area', [{
        height: 0
      },
      {
        height: "300px"
      }
    ], 110)
  },
  nameOut(){
    this.animate('.area', [{
      height: "300px"
    },
    {
      height: 0
    }
  ], 110)
  },
  nameComfirm(result){
    console.log(result)
    this.setData({
      ['name']:result.detail.values[0].name+'/'+result.detail.values[1].name+'/'+result.detail.values[2].name
    })
    this.nameOut()
  },
  chooseTime(e){  
    const Class=e.currentTarget.dataset.classname
    console.log(Class)
    this.animate(Class, [{
      height: 0
    },
    {
      height: "300px"
    }
  ], 110)
  },
  timeComfirm(result){
    const time=result.currentTarget.dataset.time
    this.setData({
      time:`${new Date(result.detail.value).getFullYear()}/${new Date(result.detail.value).getMonth()+1}/${new Date(result.detail.value).getDate()}`
    })
    console.log("hhhh")
    this.outTime()
  },
  outTime(){
    this.animate(".time", [
    {
      height: 0
    }
  ], 110)
  },
  
  onReachBottom(){
    this.getData()
    this.getUncheckData()
  },
  onLoad(options) {
    this._index=0
    this.setData({
      vanTop: app.globalData.vanTop,
    })
    this.getData()
    this.getUncheckData()
  },
  call(e){
    const phone=e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })
  },
  delete(e){
    const id=e.currentTarget.dataset.doc
    wx.showModal({
      title: '删除确认',
      content: '删除该条数据后将无法恢复',
      success: (res)=>{
        if (res.confirm) {
          db.collection("snapShot").doc(id).remove({
            success:()=>{
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000,
              })
              this.resect()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
    
  },
  replyCall(e){
    const item=e.currentTarget.dataset.item
    const reply=e.detail.value
    let index=0
    if(item.state===1){
      this.data.contentList.map(i=>{
        if(i._id===item._id){
          this.setData({
            [`contentList[${index}].reply`]:reply
          })
        }
        index++
      })
    }else{
      this.data.uncheckContentList.map(i=>{
        if(i._id===item._id){
          this.setData({
            [`uncheckContentList[${index}].reply`]:reply
          })
        }
        index++
      })
    }
    console.log(this.data.contentList)
    console.log(this.data)
  },
  accept(e){
    wx.showModal({
      title: '发布确认',
      content: '点击确定将发布此条随手拍',
      success: (res)=>{
        if (res.confirm) {
          const index=e.currentTarget.dataset.index
          const item=this.data.uncheckContentList[index]
          console.log(item.reply)
          db.collection("snapShot").doc(`${item._id}`).update({
            data:{
              state:1,
              reply:item.reply
            },
            success:(res)=>{
              wx.showToast({
                title: '发布成功',
                icon: 'success',
                duration: 2000,
              })
              this.resect()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  reject(e){
    wx.showModal({
      title: '打回确认',
      content: '点击确定将打回此条随手拍',
      success: (res)=>{
        if (res.confirm) {
          const index=e.currentTarget.dataset.index
          const item=this.data.uncheckContentList[index]
          console.log(item.reply)
          db.collection("snapShot").doc(`${item._id}`).update({
            data:{
              state:-1,
              reply:item.reply
            },
            success:(res)=>{
              wx.showToast({
                title: '打回成功',
                icon: 'success',
                duration: 2000,
              })
              this.resect()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  changeReply(e){
    
      wx.showModal({
        title: '修改确认',
        content: '点击确定将修改本条随手拍官方回复',
        success: (res)=>{
          if (res.confirm) {
            const index=e.currentTarget.dataset.index
            const item=this.data.contentList[index]
            console.log(item.reply)
            db.collection("snapShot").doc(`${item._id}`).update({
              data:{
                reply:item.reply
              },
              success:(res)=>{
                wx.showToast({
                  title: '修改成功',
                  icon: 'success',
                  duration: 2000,
                })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
  },
  resect(){
    this._index=0
    this.setData({
      contentList:[],
      uncheckContentList:[],
      name:"",
      descri:"",
      time:"",
    })
    this.getUncheckData()
    this.getData()
  },
  search(){
    this._index=0
    this.setData({
      contentList:[],
      uncheckContentList:[]
    })
    this.getUncheckData()
    this.getData()
  },
  getData(){
    this.setData({
      loading:true
    })
    // 模糊查询
    let searchData={
      name:db.RegExp({
        regexp: '.*' + this.data.name,
        options: 'i',
      }),
      descri:db.RegExp({
        regexp: '.*' + this.data.descri,
        options: 'i',
      }),
      time:this.data.time,
      state:1
    }
    
    if(this.data.name===""){
      delete searchData.name
    }
    if(this.data.descri===""){
      delete searchData.descri
    }
    if(this.data.time===""){
      delete searchData.time
    }
    db.collection("snapShot").orderBy("time","asc").where(searchData).skip(this._index*6).limit(6).get({
      success:(res)=>{
      console.log(res)
       if(res.data.length!==0){
        this.setData({
          contentList:this.data.contentList.concat(res.data)
         })
       }
       this.setData({
        loading:false
      })
       this._index++
      }
    })
  },
  getUncheckData(){
    db.collection("snapShot").orderBy("time","asc").where({state:0}).skip(this._index*6).limit(6).get({
      success:(res)=>{
      console.log(res)
       if(res.data.length!==0){
        this.setData({
          uncheckContentList:this.data.uncheckContentList.concat(res.data)
         })
       }
       this.setData({
        loading:false
      })
       this._index++
      }
    })
  },

  nothing(){}

})